﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EFEntity;
using Iservices;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Model;
using Newtonsoft.Json;

namespace Hr.Controllers
{
    public class resumeController : Controller
    {
        private readonly ILogger<clientController> _logger;
        private readonly MyDbContext myDbContext;
        private readonly Iconfig_major_kindServices mkServices;
        private readonly Iconfig_majorServices mServices;
        private readonly Iengage_resumeServices erServices;

        public resumeController(MyDbContext myDbContext, ILogger<clientController> logger, Iconfig_major_kindServices mkServices, Iconfig_majorServices mServices, Iengage_resumeServices erServices)
        {
            this.myDbContext = myDbContext;
            _logger = logger;
            this.mkServices = mkServices;
            this.mServices = mServices;
            this.erServices = erServices;
        }



        [HttpGet]
        public IActionResult register(string major_kind_id, string major_kind_name, string major_id, string major_name, string engage_type)//跳转到简历登记页面
        {
            ViewData["major_kind_id"] = major_kind_id;
            ViewData["major_kind_name"] = major_kind_name;
            ViewData["major_id"] = major_id;
            ViewData["major_name"] = major_name;
            ViewData["engage_type"] = engage_type;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> register([FromForm] Eengage_resume test)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                try
                {
                    test.register = "zhangsan";
                    myDbContext.engage_resume.Add(test);
                    result = await myDbContext.SaveChangesAsync();
                    _logger.LogInformation("成功");
                }
                catch (Exception ex)
                {
                    _logger.LogWarning(ex.Message);
                }
            }
            return Content(result.ToString());     
        }

        [HttpPost]
        public async Task<ActionResult> GetAll()
        {
            List<config_major_kind> major_kind = await mkServices.config_major_kindSelect();
            List<config_major> major = await mServices.config_majorSelect();
            Dictionary<string, object> di = new Dictionary<string, object>();
            di["major_kind"] = major_kind;
            di["major"] = major;
            return Content(JsonConvert.SerializeObject(di));
        }

        [HttpPost]
        //职位名称 根据选择职位分类查询职位名称
        public async Task<ActionResult> Getmajor(string id)
        {
            List<config_major> major = await mServices.Getmajor(id);
            return Content(JsonConvert.SerializeObject(major));
        }

        [HttpGet]
        public ActionResult resume_choose()
        {
            return View();
        }

        //简历查询
        [HttpGet]
        public IActionResult resume_list(string major_kind_id, string major_id, string primarKey, string startDate, string endDate)//跳转到筛选简历筛选列表
        {
            string where = "1=1";
            if (major_kind_id != null)
            {
                if (Convert.ToInt32(major_kind_id) > 0)
                {
                    where += $"and [human_major_kind_id] ='{major_kind_id}'";
                }
            }
            if (major_id != null)
            {
                if (Convert.ToInt32(major_id) > 0)
                {
                    where += $"and [human_major_id] ='{major_id}'";
                }
            }
            if (primarKey != null)
            {
                where += $"and [register] like '%{primarKey}%' or [checker] like '%{primarKey}%' ";
            }
            if (startDate != null && endDate != null)
            {
                where += $"and [regist_time] between '{startDate}'and '{endDate}' ";
            }
            else if (startDate != null)
            {
                where += $"and [regist_time] >= '{startDate}' ";
            }
            else if (endDate != null)
            {
                where += $"and [regist_time] >= '{endDate}' ";
            }
            ViewData["where"] = where;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> resume_list(int currentPage,string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }

        [HttpGet]
        public ActionResult resume_details(int id)
        {
            ViewData["id"] = id;
            return View();
        }

        [HttpPost]
        public async Task<ActionResult> resume_details1(int id)
        {
            var fd = await myDbContext.engage_resume.FindAsync(id);
            return Content(JsonConvert.SerializeObject(fd));
        }
        [HttpPut]
        public async Task<IActionResult> resume_details([FromForm] Eengage_resume test)
        {
            int result;
            if (ModelState.IsValid)
            {
                test.check_status = 1;
                myDbContext.engage_resume.Attach(test);
                myDbContext.Entry(test).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                result = await myDbContext.SaveChangesAsync();
            }
            else
            {
                var item = ModelState.Values.Take(1).SingleOrDefault();
                return Ok(new ResultInfo() { Msg = item.Errors });
            }

            return Content(result.ToString());
        }


        [HttpGet]
        public ActionResult valid_resume()
        {
            return View();
        }

        //简历查询
        [HttpGet]
        public IActionResult valid_list(string major_kind_id, string major_id, string primarKey, string startDate, string endDate)//跳转到筛选简历筛选列表
        {
            string where = "1=1 and [check_status] = 1 and [interview_status] = 0";
            if (major_kind_id != null)
            {
                if (Convert.ToInt32(major_kind_id) > 0)
                {
                    where += $"and [human_major_kind_id] ='{major_kind_id}'";
                }
            }
            if (major_id != null)
            {
                if (Convert.ToInt32(major_id) > 0)
                {
                    where += $"and [human_major_id] ='{major_id}'";
                }
            }
            if (primarKey != null)
            {
                where += $"and [register] like '%{primarKey}%' or [checker] like '%{primarKey}%' ";
            }
            if (startDate != null && endDate != null)
            {
                where += $"and [regist_time] between '{startDate}'and '{endDate}' ";
            }
            else if (startDate != null)
            {
                where += $"and [regist_time] >= '{startDate}' ";
            }
            else if (endDate != null)
            {
                where += $"and [regist_time] >= '{endDate}' ";
            }
            ViewData["where"] = where;
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> valid_list(int currentPage, string where)
        {
            Dictionary<string, object> list = await erServices.FenYe_resume_Async(currentPage, where);
            return Content(JsonConvert.SerializeObject(list));
        }



    }
}
